
using System;
using System.Windows.Input;
using MonoDevelop.DesignerSupport.Toolbox;
using MonoDevelop.XamlDesigner.Controller;
using System.Windows.Controls;
using System.Windows;

namespace MonoDevelop.XamlDesigner.Model
{


	public class DragedControlTool : AbstractTool
	{

		public DragedControlTool(MoonlightController controller):
			base (controller)
		{
		}
        
        private ItemToolboxNode draggedItem;
        private Gtk.Widget draggedSource;
        private Gdk.DragContext dragContext;
        private bool draggingFromToolbox;
        
		public override void MouseUp (object sender, MouseEventArgs ev)
		{
            if (!draggingFromToolbox) {
                base.MouseUp(sender, ev);
            	return;
            }
            //else
            Console.WriteLine("QUICK HACK: Drop on moonlightcontroler");
            TypeToolboxNode typetoolnode = draggedItem as TypeToolboxNode;
            if (typetoolnode != null)
            {
                Type type = typetoolnode.Type.Load ();
                if (typeof(System.Windows.FrameworkElement).IsAssignableFrom (type)) {
	                System.Windows.FrameworkElement obj = (System.Windows.FrameworkElement)System.Activator.CreateInstance(type, new object[] {});
                    if (sender is Panel) {
                        //TODO: loop on parent to see if there is a parent in upper node
						((Panel)sender).Children.Add(obj);
                        createdElement = obj;
                        PushUndo();
                    }
                }                
            }
            //loop on hierarchie parent to get first panel or usercontrol and add new oject to it!
			draggingFromToolbox = false;
            
		}
        private FrameworkElement createdElement;
        
		protected override void PushUndo ()
		{
            if (!draggingFromToolbox) 
            {
                base.PushUndo();
                return;
            }
            //else we are dragging a tool item from toolbox
			IUndoAction undo = new UndoAddObject(((Panel)createdElement.Parent).Children, createdElement);
			Controller.UndoEngine.PushUndo(undo);
		}

		public void BeginDrag (MonoDevelop.DesignerSupport.Toolbox.ItemToolboxNode item, Gtk.Widget source, Gdk.DragContext ctx)
		{
			//TODO use the regular drag and drop system
            dragContext = ctx;
            draggedItem = item;
            draggedSource = source;
            draggingFromToolbox = true;
		}
        
        public override void Activate()
		{
			Controller.Selection.Show();
		}
        
        public override void Deactivate ()
		{
			Controller.Selection.Hide();
		}
        
        public override void MouseDown (object sender, MouseEventArgs ev)
		{
			base.MouseDown (sender, ev);
			
			UIElement clicked_element = sender as UIElement;
            
			if (clicked_element != null) {
				if (!Controller.Selection.Contains(clicked_element) ){
					Controller.Selection.Clear();
					Controller.Selection.Add(clicked_element);
                    //TODO notifie combotreebox
				}
				return;
			}
		}
	}
}
